import Vue from 'vue'
import Router from 'vue-router'
import store from "../vuex";

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const Home = () => import('@/components/home')
const Login = () => import('@/components/login')

const Welcome = () => import('@/components/navList/vWelcome')

const MenuList = () => import('@/components/menu/vMenuList')
const MenuAdd = () => import('@/components/menu/vMenuAdd')

const RoleList = () => import('@/components/role/vRoleList')
const RoleAdd = () => import('@/components/role/vRoleAdd')

const UserList = () => import('@/components/user/vUserList')
const UserAdd = () => import('@/components/user/vUserAdd')

const CategoryList = () => import('@/components/category/vCategoryList')
const CategoryAdd = () => import('@/components/category/vCategoryAdd')

const SpecsList = () => import('@/components/specs/vSpecsList')
const SpecsAdd = () => import('@/components/specs/vSpecsAdd')

const GoodsList = () => import('@/components/goods/vGoodsList')
const GoodsAdd = () => import('@/components/goods/vGoodsAdd')

const SeckList = () => import('@/components/seck/vSeckList')
const SeckAdd = () => import('@/components/seck/vSeckAdd')

const BannerList = () => import('@/components/banner/vBannerList')
const BannerAdd = () => import('@/components/banner/vBannerAdd')

const MemberList = () => import('@/components/member/vMemberList')
const MemberAdd = () => import('@/components/member/vMemberAdd')
Vue.use(Router)
const routes = [
  {path: '/', redirect: '/home'},
  {
    path: '/home',
    component: Home,
    name: '首页',
    children: [
      {path: '', component: Welcome, meta: {tag:'/home'},name: '欢迎界面'},
      {
        path: 'menulist', component: MenuList, name: '菜单列表', meta: {tag:'/home/menulist'}, children: [
          {path: 'menuadd', component: MenuAdd, name: '菜单添加', meta: {tag:'/home/menulist'}},
          {path: 'menuedit/:id', component: MenuAdd, name: '编辑菜单 ', meta: {tag:'/home/menulist'}}
        ]
      },
      {path: 'rolelist', component: RoleList, name: '角色管理', meta: {tag:'/home/rolelist'}, children: [
          {path: 'roleadd', component: RoleAdd, name: '角色添加', meta: {tag:'/home/rolelist'}},
          {path: 'roleedit/:id', component: RoleAdd, name: '角色菜单 ', meta: {tag:'/home/rolelist'}}
        ]
      },
      {path: 'userlist', component: UserList, name: '管理员管理', meta: {tag:'/home/userlist'}, children: [
          {path: 'useradd', component: UserAdd, name: '管理员添加', meta: {tag:'/home/userlist'}},
          {path: 'useredit/:id', component: UserAdd, name: '管理员菜单 ', meta: {tag:'/home/userlist'}}
        ]
      },
      {path: 'categorylist', component: CategoryList, name: '商品分类列表', meta: {tag:'/home/categorylist'}, children: [
          {path: 'categoryadd', component: CategoryAdd, name: '商品分类添加', meta: {tag:'/home/categorylist'}},
          {path: 'categoryedit/:id', component: CategoryAdd, name: '商品分类菜单 ', meta: {tag:'/home/categorylist'}}
        ]
      },
      {path: 'specslist', component: SpecsList, name: '商品规格列表', meta: {tag:'/home/specslist'}, children: [
          {path: 'specsadd', component: SpecsAdd, name: '商品规格添加', meta: {tag:'/home/specslist'}},
          {path: 'specsedit/:id', component: SpecsAdd, name: '商品规格菜单 ', meta: {tag:'/home/specslist'}}
        ]
      },
      {path: 'goodslist', component: GoodsList, name: '商品列表', meta: {tag:'/home/goodslist'}, children: [
          {path: 'goodsadd', component: GoodsAdd, name: '商品添加', meta: {tag:'/home/goodslist'}},
          {path: 'goodsedit/:id', component: GoodsAdd, name: '商品菜单 ', meta: {tag:'/home/goodslist'}}
        ]
      },
      {path: 'secklist', component: SeckList, name: '限时秒杀列表', meta: {tag:'/home/secklist'}, children: [
          {path: 'seckadd', component: SeckAdd, name: '限时秒杀添加', meta: {tag:'/home/secklist'}},
          {path: 'seckedit/:id', component: SeckAdd, name: '限时秒杀菜单 ', meta: {tag:'/home/secklist'}}
        ]
      },
      {path: 'bannerlist', component: BannerList, name: '轮播图列表', meta: {tag:'/home/bannerlist'}, children: [
          {path: 'banneradd', component: BannerAdd, name: '轮播图添加', meta: {tag:'/home/bannerlist'}},
          {path: 'banneredit/:id', component: BannerAdd, name: '轮播图菜单 ', meta: {tag:'/home/bannerlist'}}
        ]
      },
      {path: 'memberlist', component: MemberList, name: '会员列表', meta: {tag:'/home/memberlist'}, children: [
          {path: 'memberadd', component: MemberAdd, name: '会员添加', meta: {tag:'/home/memberlist'}},
          {path: 'memberedit/:id', component: MemberAdd, name: '会员菜单 ', meta: {tag:'/home/memberlist'}}
        ]
      },
    ]
  },
  {path: '/login',component: Login},
  {path: '*', redirect: '/'}
]

const router = new Router({
  routes,
})
router.beforeEach((to, from, next) =>{
  let token = store.state.userInfo.token
  if(to.fullPath != '/login') {
    //当访问的路由不是login时，判断是否有token
    if(token) {//有 token
      let nenus_url = store.state.userInfo.menus_url
      if(nenus_url.indexOf('/home') == -1) {
        nenus_url.push('/home')
      }
      let currentPath
      if(to.fullPath.split('/').length>=3) {
        currentPath = '/'+ to.fullPath.split('/')[1] + '/' +to.fullPath.split('/')[2]
      }else{
        currentPath ='/'+ to.fullPath.split('/')[1]
      }
      if(nenus_url.indexOf(currentPath) !== -1) {
        next()
      }else{
        next('/')
      }
    }else{//么有 token
      next('/login')
    }
  }else{//访问的路由是 /login 时
    next()
  }
})

export default router
